package pl.gigiel.behaviour.util;

import com.jme.math.Vector3f;

public class BorderGuard {
	private Float xMax;
	private Float xMin;
	private Float yMax;
	private Float yMin;
	private Float zMax;
	private Float zMin;

	public BorderGuard() {
	}

	public void set(Vector3f cornerMin, Vector3f cornerMax) {
		this.xMin = cornerMin.x;
		this.yMin = cornerMin.y;
		this.zMin = cornerMin.z;
		this.xMax = cornerMax.x;
		this.yMax = cornerMax.y;
		this.zMax = cornerMax.z;
	}

	public void setXMax(Float max) {
		xMax = max;
	}

	public void setXMin(Float min) {
		xMin = min;
	}

	public void setYMax(Float max) {
		yMax = max;
	}

	public void setYMin(Float min) {
		yMin = min;
	}

	public void setZMax(Float max) {
		zMax = max;
	}

	public void setZMin(Float min) {
		zMin = min;
	}

	public Vector3f correctedPositionLocal(Vector3f position) {
		// <preconditions>
		Util.checkNaN(position);
		// </preconditions>
		position.setX(corrected(position.getX(), xMin, xMax));
		position.setY(corrected(position.getY(), yMin, yMax));
		position.setZ(corrected(position.getZ(), zMin, zMax));
		return position;
	}

	private static float corrected(float position, Float min, Float max) {
		float result = position;
		if (min != null && position < min) {
			result = min;
		} else if (max != null && position > max) {
			result = max;
		}
		return result;
	}
}
